package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.commo.utils.R;
import com.qf.entity.Permission;
import com.qf.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;


    //树形结构菜单
    @GetMapping("/treeList")
    public R treeList(){

        //后端返回的是简单的JSON格式

        return R.putDate("data",permissionService.treeList());
    }
    //    添加权限数据
    @PostMapping("/savePermission")
    public R save(@RequestBody Permission permission){
        return R.out(permissionService.save(permission));
    }
    //分页
    @PostMapping("/page")
    public R page(@RequestBody Page<Permission> page){
        page =permissionService.page(page);
        return R.putDate("page",page);
    }
    //通过id查询用户
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Integer id){

        Permission permission = permissionService.getById(id);
        Permission pPermission = permissionService.getById(permission.getPid());
        permission.setPname(pPermission.getName());
        return R.putDate("data",permission);
    }
    //删除用户
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        boolean b = permissionService.removeById(id);
        return R.out(b);
    }
    @PostMapping("/update")
    public R update(@RequestBody Permission permission){
        return R.out(permissionService.updateById(permission));
    }

}
